using System.Data;
using AnhAnh;
using FacebookLib;
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Collections.Generic;

namespace AutoSales
{
	public class FormGroupManage : Form
	{
		private FormMain formMain_0;

		private TreeNode treeNode_0;

		private bool bool_0;

		private IContainer icontainer_0;

		private TreeView treeView1;

		private GroupBox groupBox1;

		private Button buttonEdit;

		private ComboBox comboBox1;

		private Button buttonRemove;

		private Button buttonAdd;

		private Button buttonMove;

		private Label label1;

		public FormGroupManage(FormMain form)
		{
			this.InitializeComponent();
			this.formMain_0 = form;
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			TreeNode treeNode = this.treeView1.Nodes[0].Nodes.Add(string.Empty);
			this.treeView1.LabelEdit = true;
			if (!treeNode.IsEditing)
			{
				this.bool_0 = false;
				treeNode.BeginEdit();
			}
		}

		private void buttonEdit_Click(object sender, EventArgs e)
		{
			if (this.treeNode_0 == null || this.treeNode_0.Level != 1 || this.treeNode_0.Parent == null || this.treeNode_0 == this.treeView1.Nodes[0].Nodes[0])
			{
				UI.ShowMessage("No tree node selected or selected node is a root node.\nEditing of root nodes is not allowed.", "Invalid selection", UI.MessageType.Error);
			}
			else
			{
				this.treeView1.SelectedNode = this.treeNode_0;
				this.treeView1.LabelEdit = true;
				if (!this.treeNode_0.IsEditing)
				{
					this.bool_0 = true;
					this.treeNode_0.BeginEdit();
					return;
				}
			}
		}

		private void buttonMove_Click(object sender, EventArgs e)
		{
			if (this.comboBox1.SelectedItem == null)
			{
				return;
			}
			AutoSalesContext autoSalesContext = new AutoSalesContext();
			int id = ((Category)this.comboBox1.SelectedItem).Id;
			foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)
			{
				for (int i = node.Nodes.Count - 1; i >= 0; i--)
				{
					FormGroupManage.Class8 class8 = new FormGroupManage.Class8()
					{
						treeNode_0 = node.Nodes[i]
					};
					if (class8.treeNode_0.Level == 2 && class8.treeNode_0.Checked)
					{
						DbSet<Group> groups = autoSalesContext.Groups;
						ParameterExpression parameterExpression = Expression.Parameter(typeof(Group), "m");
						BinaryExpression binaryExpression = Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_FacebookId").MethodHandle)), Expression.Property(Expression.Field(Expression.Constant(class8), FieldInfo.GetFieldFromHandle(typeof(FormGroupManage.Class8).GetField("treeNode_0").FieldHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(TreeNode).GetMethod("get_Name").MethodHandle)), false, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("op_Equality", new Type[] { typeof(string), typeof(string) }).MethodHandle));
						ParameterExpression[] parameterExpressionArray = new ParameterExpression[] { parameterExpression };
						Group group = groups.Where<Group>(Expression.Lambda<Func<Group, bool>>(binaryExpression, parameterExpressionArray)).FirstOrDefault<Group>();
						if (group == null)
						{
							Group group1 = new Group();
							group1.FacebookId=(class8.treeNode_0.Name);
							group1.Name=(class8.treeNode_0.Text);
							group1.CategoryId=(new int?(id));
							object obj = group1;
							if (FormGroupManage.Class9.callSite_0 == null)
							{
								Type type = typeof(FormGroupManage);
								CSharpArgumentInfo[] cSharpArgumentInfoArray = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
								FormGroupManage.Class9.callSite_0 = CallSite<Action<CallSite, DbSet<Group>, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Add", null, type, cSharpArgumentInfoArray));
							}
							FormGroupManage.Class9.callSite_0.Target(FormGroupManage.Class9.callSite_0, autoSalesContext.Groups, obj);
						}
						else
						{
							group.CategoryId=(new int?(id));
							autoSalesContext.Entry<Group>(group).State = EntityState.Modified;
						}
						autoSalesContext.SaveChanges();
						class8.treeNode_0.Remove();
						class8.treeNode_0.Checked = false;
						this.treeView1.Nodes[0].Nodes[id.ToString()].Nodes.Add(class8.treeNode_0);
					}
				}
			}
		}

		private void buttonRemove_Click(object sender, EventArgs e)
		{
			if (this.treeNode_0 != null && this.treeNode_0.Level == 1 && UI.ConfirmDelete())
			{
				AutoSalesContext autoSalesContext = new AutoSalesContext();
				DbSet<Category> categories = autoSalesContext.Categories;
				object[] objArray = new object[] { int.Parse(this.treeNode_0.Name) };
				Category category = categories.Find(objArray);
				//autoSalesContext.Groups.Remove(category.Groups);
				autoSalesContext.Categories.Remove(category);
				autoSalesContext.SaveChanges();
				this.treeView1.Nodes.Remove(this.treeNode_0);
				this.method_0(autoSalesContext);
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.icontainer_0 != null)
			{
				this.icontainer_0.Dispose();
			}
			base.Dispose(disposing);
		}

		private void FormGroupManage_Shown(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			Application.DoEvents();
			AutoSalesContext autoSalesContext = new AutoSalesContext();
			this.method_0(autoSalesContext);
			DbSet<Category> categories = autoSalesContext.Categories;
			ParameterExpression parameterExpression = Expression.Parameter(typeof(Category), "m");
			MemberExpression memberExpression = Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Category).GetMethod("get_Title").MethodHandle));
			ParameterExpression[] parameterExpressionArray = new ParameterExpression[] { parameterExpression };
			foreach (Category category in categories.OrderBy<Category, string>(Expression.Lambda<Func<Category, string>>(memberExpression, parameterExpressionArray)))
			{
				TreeNodeCollection nodes = this.treeView1.Nodes[0].Nodes;
				int id = category.Id;
				nodes.Add(id.ToString(), category.Title);
			}
			foreach (GroupData mGroup in this.formMain_0.mGroups)
			{
				FormGroupManage.Class7 class7 = new FormGroupManage.Class7();
				Application.DoEvents();
				class7.string_0 = mGroup.id;
				DbSet<Group> groups = autoSalesContext.Groups;
				ParameterExpression parameterExpression1 = Expression.Parameter(typeof(Group), "m");
				int? nullable = null;
				BinaryExpression binaryExpression = Expression.AndAlso(Expression.Equal(Expression.Property(parameterExpression1, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_FacebookId").MethodHandle)), Expression.Field(Expression.Constant(class7), FieldInfo.GetFieldFromHandle(typeof(FormGroupManage.Class7).GetField("string_0").FieldHandle)), false, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("op_Equality", new Type[] { typeof(string), typeof(string) }).MethodHandle)), Expression.NotEqual(Expression.Property(parameterExpression1, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_CategoryId").MethodHandle)), Expression.Constant(nullable, typeof(int?))));
				ParameterExpression[] parameterExpressionArray1 = new ParameterExpression[] { parameterExpression1 };
				Group group = groups.Where<Group>(Expression.Lambda<Func<Group, bool>>(binaryExpression, parameterExpressionArray1)).FirstOrDefault<Group>();
				if (group == null)
				{
					TreeNode treeNode = new TreeNode(mGroup.name)
					{
						Name = mGroup.id,
						ToolTipText = this.method_1(mGroup)
					};
					TreeNode treeNode1 = treeNode;
					this.treeView1.Nodes[0].Nodes[0].Nodes.Add(treeNode1);
				}
				else
				{
					TreeNode treeNode2 = new TreeNode(group.Name)
					{
						Name = group.FacebookId,
						ToolTipText = this.method_1(mGroup)
					};
					TreeNode treeNode3 = treeNode2;
					TreeNodeCollection treeNodeCollections = this.treeView1.Nodes[0].Nodes;
					int? categoryId = group.CategoryId;
					treeNodeCollections[categoryId.ToString()].Nodes.Add(treeNode3);
				}
			}
			foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)
			{
				TreeNode treeNode4 = node;
				object text = treeNode4.Text;
				object[] count = new object[] { text, " (", node.Nodes.Count, ")" };
				treeNode4.Text = string.Concat(count);
			}
			if (this.treeView1.Nodes[0].Nodes.Count != 1)
			{
				this.treeView1.Nodes[0].Expand();
			}
			else
			{
				this.treeView1.ExpandAll();
			}
			this.Cursor = Cursors.Default;
			Application.DoEvents();
		}

		private void InitializeComponent()
		{
			TreeNode treeNode = new TreeNode("Uncategorized");
			TreeNode treeNode1 = new TreeNode("All", new TreeNode[] { treeNode });
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(FormGroupManage));
			this.treeView1 = new TreeView();
			this.groupBox1 = new GroupBox();
			this.buttonEdit = new Button();
			this.buttonRemove = new Button();
			this.buttonAdd = new Button();
			this.comboBox1 = new ComboBox();
			this.buttonMove = new Button();
			this.label1 = new Label();
			this.groupBox1.SuspendLayout();
			base.SuspendLayout();
			this.treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
			this.treeView1.CheckBoxes = true;
			this.treeView1.Location = new Point(12, 12);
			this.treeView1.Name = "treeView1";
			treeNode.Name = "NodeUncategorized";
			treeNode.Text = "Uncategorized";
			treeNode1.Name = "NodeAll";
			treeNode1.Text = "All";
			this.treeView1.Nodes.AddRange(new TreeNode[] { treeNode1 });
			this.treeView1.ShowNodeToolTips = true;
			this.treeView1.Size = new System.Drawing.Size(382, 397);
			this.treeView1.TabIndex = 0;
			this.treeView1.AfterLabelEdit += new NodeLabelEditEventHandler(this.treeView1_AfterLabelEdit);
			this.treeView1.AfterCheck += new TreeViewEventHandler(this.treeView1_AfterCheck);
			this.treeView1.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseDoubleClick);
			this.treeView1.MouseDown += new MouseEventHandler(this.treeView1_MouseDown);
			this.groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			this.groupBox1.Controls.Add(this.buttonEdit);
			this.groupBox1.Controls.Add(this.buttonRemove);
			this.groupBox1.Controls.Add(this.buttonAdd);
			this.groupBox1.Location = new Point(398, 6);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(83, 153);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Category";
			this.buttonEdit.Location = new Point(4, 65);
			this.buttonEdit.Name = "buttonEdit";
			this.buttonEdit.Size = new System.Drawing.Size(75, 35);
			this.buttonEdit.TabIndex = 5;
			this.buttonEdit.Text = "Edit";
			this.buttonEdit.UseVisualStyleBackColor = true;
			this.buttonEdit.Click += new EventHandler(this.buttonEdit_Click);
			this.buttonRemove.Location = new Point(4, 108);
			this.buttonRemove.Name = "buttonRemove";
			this.buttonRemove.Size = new System.Drawing.Size(75, 35);
			this.buttonRemove.TabIndex = 6;
			this.buttonRemove.Text = "Remove";
			this.buttonRemove.UseVisualStyleBackColor = true;
			this.buttonRemove.Click += new EventHandler(this.buttonRemove_Click);
			this.buttonAdd.Location = new Point(4, 22);
			this.buttonAdd.Name = "buttonAdd";
			this.buttonAdd.Size = new System.Drawing.Size(75, 35);
			this.buttonAdd.TabIndex = 4;
			this.buttonAdd.Text = "Add";
			this.buttonAdd.UseVisualStyleBackColor = true;
			this.buttonAdd.Click += new EventHandler(this.buttonAdd_Click);
			this.comboBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
			this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
			this.comboBox1.FormattingEnabled = true;
			this.comboBox1.Location = new Point(85, 418);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(228, 21);
			this.comboBox1.TabIndex = 1;
			this.buttonMove.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
			this.buttonMove.Location = new Point(319, 416);
			this.buttonMove.Name = "buttonMove";
			this.buttonMove.Size = new System.Drawing.Size(75, 25);
			this.buttonMove.TabIndex = 2;
			this.buttonMove.Text = "Move";
			this.buttonMove.UseVisualStyleBackColor = true;
			this.buttonMove.Click += new EventHandler(this.buttonMove_Click);
			this.label1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
			this.label1.AutoSize = true;
			this.label1.Location = new Point(9, 422);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(74, 13);
			this.label1.TabIndex = 11;
			this.label1.Text = "Categorization";
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(486, 447);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.groupBox1);
			base.Controls.Add(this.comboBox1);
			base.Controls.Add(this.treeView1);
			base.Controls.Add(this.buttonMove);
			base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
			base.Name = "FormGroupManage";
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "Facebook Groups Management";
			base.Shown += new EventHandler(this.FormGroupManage_Shown);
			this.groupBox1.ResumeLayout(false);
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		private void method_0(AutoSalesContext autoSalesContext_0)
		{
			DbSet<Category> categories = autoSalesContext_0.Categories;
			ParameterExpression parameterExpression = Expression.Parameter(typeof(Category), "m");
			MemberExpression memberExpression = Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Category).GetMethod("get_Title").MethodHandle));
			ParameterExpression[] parameterExpressionArray = new ParameterExpression[] { parameterExpression };
			IOrderedQueryable<Category> categories1 = categories.OrderBy<Category, string>(Expression.Lambda<Func<Category, string>>(memberExpression, parameterExpressionArray));
			this.comboBox1.DisplayMember = "Title";
			this.comboBox1.ValueMember = "Id";
			this.comboBox1.DataSource = categories1.ToList<Category>();
		}

		private string method_1(GroupData groupData_0)
		{
			string[] groupData0 = new string[] { groupData_0.name, Environment.NewLine, "Privacy: ", groupData_0.privacy, Environment.NewLine, "Email: ", groupData_0.email, Environment.NewLine, "Double click to open: https://www.facebook.com/groups/", groupData_0.id };
			return string.Concat(groupData0);
		}

		private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
		{
			foreach (TreeNode node in e.Node.Nodes)
			{
				node.Checked = e.Node.Checked;
			}
		}

		private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			string str = (e.Label != null ? e.Label : e.Node.Text);
			if (string.IsNullOrEmpty(str))
			{
				e.CancelEdit = true;
				MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", "Node Label Edit");
				e.Node.BeginEdit();
				return;
			}
			if (str.Length <= 0)
			{
				e.CancelEdit = true;
				MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", "Node Label Edit");
				e.Node.BeginEdit();
				return;
			}
			if (str.IndexOfAny(new char[] { '@', '.', ',', '!' }) != -1)
			{
				e.CancelEdit = true;
				MessageBox.Show("Invalid tree node label.\nThe invalid characters are: '@','.', ',', '!'", "Node Label Edit");
				e.Node.BeginEdit();
				return;
			}
			e.Node.EndEdit(false);
			AutoSalesContext autoSalesContext = new AutoSalesContext();
			if (!this.bool_0)
			{
				Category category = new Category();
				category.Title=(str);
				object obj = category;
				if (FormGroupManage.Class10.callSite_0 == null)
				{
					Type type = typeof(FormGroupManage);
					CSharpArgumentInfo[] cSharpArgumentInfoArray = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
					FormGroupManage.Class10.callSite_0 = CallSite<Action<CallSite, DbSet<Category>, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Add", null, type, cSharpArgumentInfoArray));
				}
				FormGroupManage.Class10.callSite_0.Target(FormGroupManage.Class10.callSite_0, autoSalesContext.Categories, obj);
				autoSalesContext.SaveChanges();
				TreeNode node = e.Node;
				if (FormGroupManage.Class10.callSite_1 == null)
				{
					FormGroupManage.Class10.callSite_1 = CallSite<Func<CallSite, object, string>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(FormGroupManage)));
				}
				var target = FormGroupManage.Class10.callSite_1.Target;
				CallSite<Func<CallSite, object, string>> callSite1 = FormGroupManage.Class10.callSite_1;
				if (FormGroupManage.Class10.callSite_2 == null)
				{
					Type type1 = typeof(FormGroupManage);
					CSharpArgumentInfo[] cSharpArgumentInfoArray1 = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
					FormGroupManage.Class10.callSite_2 = CallSite<Func<CallSite, Type, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, type1, cSharpArgumentInfoArray1));
				}
				var _u00210 = FormGroupManage.Class10.callSite_2.Target;
				CallSite<Func<CallSite, Type, object, object>> callSite2 = FormGroupManage.Class10.callSite_2;
				Type type2 = typeof(Convert);
				if (FormGroupManage.Class10.callSite_3 == null)
				{
					Type type3 = typeof(FormGroupManage);
					CSharpArgumentInfo[] cSharpArgumentInfoArray2 = new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
					FormGroupManage.Class10.callSite_3 = CallSite<Func<CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, "Id", type3, cSharpArgumentInfoArray2));
				}
				node.Name = target(callSite1, _u00210(callSite2, type2, FormGroupManage.Class10.callSite_3.Target(FormGroupManage.Class10.callSite_3, obj)));
			}
			else
			{
				DbSet<Category> categories = autoSalesContext.Categories;
				object[] objArray = new object[] { int.Parse(e.Node.Name) };
				Category category1 = categories.Find(objArray);
				category1.Title=(str);
				autoSalesContext.Entry<Category>(category1).State = EntityState.Modified;
				autoSalesContext.SaveChanges();
			}
			this.method_0(autoSalesContext);
		}

		private void treeView1_MouseDown(object sender, MouseEventArgs e)
		{
			this.treeNode_0 = this.treeView1.GetNodeAt(e.X, e.Y);
		}

		private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes.Count == 0 && e.Node.Name.Length > 4)
			{
				Process.Start(string.Concat("https://www.facebook.com/groups/", e.Node.Name));
			}
		}

		private static class Class10
		{
			public static CallSite<Action<CallSite, DbSet<Category>, object>> callSite_0;

			public static CallSite<Func<CallSite, object, string>> callSite_1;

			public static CallSite<Func<CallSite, Type, object, object>> callSite_2;

			public static CallSite<Func<CallSite, object, object>> callSite_3;
		}

		private sealed class Class7
		{
			public string string_0;

			public Class7()
			{
			}
		}

		private sealed class Class8
		{
			public TreeNode treeNode_0;

			public Class8()
			{
			}
		}

		private static class Class9
		{
			public static CallSite<Action<CallSite, DbSet<Group>, object>> callSite_0;
		}
	}
}